package com.kehutong.charge.app;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;

/**
 * @author joneRon
 * @Title: kht
 * @Description: 公共的接口
 * @date 2021/8/17
 */
public class ChargeCommon {

    @Inject
    private DoveClient doveClient;

    /**
     * 用户房屋列表接口
     *
     * @param jsonObject 查询条件
     * @return 住户房屋绑定列表
     * @throws Exception 异常
     */
    @ReqMapping("/house/list")
    public Object getResidentHouseList(Token token, JSONObject jsonObject) throws Exception {

        //跨库请求数据
        JSONArray result = new JSONArray();
        JSONArray khtIds = jsonObject.getJSONArray("companyNos");
        jsonObject.remove("token");
        if (Objects.nonEmpty(khtIds)) {
            for (Object khtId : khtIds) {
                String companyNo = (String) khtId;
                JSONObject residentHouseResult = doveClient.post("/crm/resident/service/getResidentHouseList", (http) -> {
                    http.addHeader("companyNo", companyNo);
                    jsonObject.put("pageSize",2000);
                    http.setBody(jsonObject.toJSONString());
                });
                if (residentHouseResult.getIntValue("code") == 200) {
                    JSONArray residentHouseList = residentHouseResult.getJSONObject("data").getJSONArray("list");
                    result.addAll(residentHouseList);
                }
            }
        }
        return result;
    }
}
